<template>
    <basic-layout title="我的收获地址" :showAddBtn="false">
        <!-- 地址列表 -->
        <van-address-list :class="{'isH':isEmpty}"
            v-model="chosenAddressId"
            :list="showList"
            default-tag-text="默认"
            add-button-text="新增收获地址"
            @add="onAdd"
            @edit="onEdit" />

        <!-- 无数据 -->
        <van-empty v-if="isEmpty" class="custom-image"
            :image="require('@/assets/images/empty.png')"
            description="暂无收获地址" />
    </basic-layout>
</template>

<script>
    import { getQuery } from '@api/platframe/mainFrame/address';
    
    export default {
        props: {

        },
        components: {

        },
        data () {
            return {
                chosenAddressId: '1',
                isEmpty: false, //是否 无数据
                showList: [],// 列表显示数据
                resData: [], // 接口返回原数据
            };
        },
        computed: {},
        watch: {},
        methods: {
            // 获取地址数据
            async getInit() {
                let obj = { staffId: sessionStorage.getItem("userid") };
                
                let res = await getQuery(obj);
                if (res && res.code === 1000) { // 处理显示数据
                    this.resData = res.data;
                    if (res.data.length>0) {
                        this.isEmpty = false;
                        let arr = [];
                        res.data.map(item=>{
                            let address =  item.provName+item.cityName+item.countyName+item.detailAddr
                            arr.push({
                                id: item.addrId,
                                name: item.receiver,
                                tel: item.receiverContact,
                                address,
                                isDefault: item.isDefault === '1',
                            })
                        })
                        this.showList = arr;
                    } else {
                        this.showList = [];
                        this.isEmpty = true;
                    }
                }
            },


            // 新增地址
            onAdd() {
                this.$router.push({ name:'shippingAddress-appEdit', params: {key: 'add'} });
            },
            // 编辑地址
            onEdit(item,index) {
                let data = { ...this.resData[index] };
                data.isDefault = data.isDefault === "1";
                
                this.$router.push({ name:'shippingAddress-appEdit', params: {key: 'edit',val:data} });
            },
        },
        created () {

        },
        mounted () {
            this.getInit();
        }
    };
</script>

<style scoped lang="less">
// ::v-deep .van-nav-bar__left{
//     i, .van-nav-bar__text{
//         color: #000;
//     }
// }

// 无数据时，不显示高度
.isH {
    height: 0 !important;
}
.van-address-list {
    height: calc(100% - 47px);
    overflow: auto;
    background-color: #F4F7FA;
    ::v-deep .van-address-item .van-radio__icon--checked .van-icon {
        background-color: #1989fa;
        border-color: #1989fa;
    }
    .van-tag--danger {
        background-color: #1989fa;
    }
}

.van-button--danger {
    background-color: #1989fa;
    border-color: #1989fa;
}
</style>